package com.gjy.mysql.type;

import com.gjy.mysql.util.ObjectUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-11-18 11:15:51
 */
public abstract class BaseTypeHandler<T> implements TypeHandler<T> {

    @Override
    public void setParameter(PreparedStatement ps, int i, T parameter, int sqlType) {
        if (ObjectUtil.isEmpty(parameter)) {
            try {
                ps.setNull(i, sqlType);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            setNonNullParameter(ps, i, parameter);
        }
    }

    @Override
    public T getResult(ResultSet rs, String columnName) {
        return getNonHullResult(rs, columnName);
    }

    @Override
    public T getResult(ResultSet rs, Integer columnIndex) {
        return getNonHullResult(rs, columnIndex);
    }

    public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter);

    public abstract T getNonHullResult(ResultSet rs, Integer columnIndex);

    public abstract T getNonHullResult(ResultSet rs, String columnName);
}
